/*******************************************************************************
 ** Government Sanctioned Espionage RPG                                       **
 ** http://www.government-sanctioned.us/                                      **
 **===========================================================================**
 ** Name: log.cpp                                                             **
 ** Description:                                           **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2012  Barry Gian James <gian@gamingods.net>            **
 ** All rights reserved.
 **                                                                           **
 ** Ref: $HeadURL$ **/

// Created on March 24, 2010, 1:02 AM
// Modified $Date$ by $Author$
#define  Log_cpp_ID    "$Id$"

#include <cstdio>
#include <pthread.h>
#include "log.hpp"

pthread_mutex_t lockLog = PTHREAD_MUTEX_INITIALIZER;

namespace debug {
Log * log;
}
//#define LOG_INFO	0
//#define LOG_CRIT	1
//#define LOG_FATAL	2
//#define LOG_DEBUG	3

void
Log::out(const char * fmt, ...)
{
    pthread_mutex_lock(&lockLog);

    char buff[512];
    std::string tmpout;
    va_list pa;

    if (flags & LOG_DATE) {
        // TODO

    } else if (flags & LOG_TIME) {
        time_t timev = time(0);
        tm  tms;
        localtime_r(&timev,&tms);
        sprintf(buff,"%02d:%02d:%02d",tms.tm_hour, tms.tm_min, tms.tm_sec);

    } else if (flags & LOG_DATETIME) {
        time_t timev = time(0);
        sprintf(buff,"%s",ctime(&timev));
    }

    fileStream << buff << ": ";
    va_start(pa,fmt);

    vsprintf(buff,fmt,pa);
    fileStream << buff << std::endl;

    va_end(pa);
    pthread_mutex_unlock(&lockLog);
}

void
Log::out(int l, const char * fmt, ...)
{
    pthread_mutex_lock(&lockLog);
    if (l >= level)
    {
         char buff[512];
        std::string tmpout;
        va_list pa;

        if (flags & LOG_DATE) {
            // TODO

        } else if (flags & LOG_TIME) {
            time_t timev = time(0);
            tm  tms;
            localtime_r(&timev,&tms);
            sprintf(buff,"%02d:%02d:%02d",tms.tm_hour, tms.tm_min, tms.tm_sec);

        } else if (flags & LOG_DATETIME) {
            time_t timev = time(0);
            sprintf(buff,"%s",ctime(&timev));
        }

        fileStream << buff << ": ";
        va_start(pa,fmt);

        vsprintf(buff,fmt,pa);
        fileStream << buff << std::endl;

        va_end(pa);
    }
    pthread_mutex_unlock(&lockLog);
}


Log::Log(const std::string & fl)
{
    fileName = fl;
    fileStream.open(fileName,std::ios::in);
    level = LOG_CRIT;
    flags = LOG_TIME;
    out("***************[LOG OPENED]****************");
    time_t timev = time(0);
    out(ctime(&timev));

}

Log::~Log()
{
    if (fileStream.is_open())
        fileStream.close();
}

